<template>
  <div class="login">
            <h3 class="title"></h3>
    <div class="login-detail">
      <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" type="text" auto-complete="off" placeholder="账号">
            <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon"/>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            auto-complete="off"
            placeholder="密码"
            show-password
            @keyup.enter.native="handleLogin"
          >
            <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon"/>
          </el-input>
        </el-form-item>
        <el-form-item prop="phonenumber">
          <el-input placeholder="请输入手机号码" v-model="loginForm.phonenumber" @keyup.enter.native="handleLogin"></el-input>
        </el-form-item>
        <el-form-item prop="code">
          <el-input placeholder="请输入验证码" v-model="loginForm.code" @keyup.enter.native="handleLogin"></el-input>
          <el-button @click.prevent="getVerifyCode" class="code-btn" :disabled="!show">
            <span v-show="show">获取验证码</span>
            <span v-show="!show" class="count">{{ count }} s</span>
          </el-button>
        </el-form-item>
        <!--        <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px; color: #ffffff;">记住密码</el-checkbox>-->
        <el-form-item style="width:100%;">
          <el-button
            :loading="loading"
            size="medium"
            type="primary"
            style="width:97%;"
            @click.prevent="handleLogin"
          >
            <!--            @click.native.prevent="handleLogin"-->
            <!--            @keyup.enter.native="handleLogin"-->
            <span v-if="!loading">登 录</span>
            <span v-else>登 录 中...</span>
          </el-button>
        </el-form-item>
      </el-form>
    </div>
    <!--  底部  -->
    <div class="el-login-footer">
      <span>Copyright © 2020-2021</span>
    </div>
  </div>
</template>

<script>
import {captchaImageByPhoneNumber, getCodeImg} from "@/api/login";
import Cookies from "js-cookie";
import {decrypt, encrypt} from "@/utils/jsencrypt";
import InputGroup from '../components/InputGroup'
import md5 from 'js-md5';
import {distributeSupervise} from "@/api/jdgl/supervise";

export default {
  name: "Login",
  components: {InputGroup},
  data() {
    return {
      codeUrl: "",
      count: 60,
      show: true,
      cookiePassword: "",
      loginForm: {
        username: "",
        password: "",
        phonenumber: "",
        rememberMe: false,
        code: "",
        uuid: "",
      },
      phone: "", //手机号
      verifyCode: "", //验证码
      btnTitle: "获取验证码",
      disabled: false,  //是否可点击
      errors: {}, //验证提示信息
      loginRules: {
        username: [
          {required: true, trigger: "blur", message: "用户名不能为空"},
        ],
        phonenumber: [
          {required: true, trigger: "blur", message: "手机号码不能为空"},
        ],
        password: [
          {required: true, trigger: "blur", message: "密码不能为空"},
        ],
        code: [
          {required: true, trigger: "change", message: "验证码不能为空"},
        ],
      },
      loading: false,
      redirect: undefined,
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true,
    },
  },
  created() {
    // this.getCode();
    // this.getCookie();
  },
  methods: {
    // 获取短信验证码
    getVerifyCode() {       //axios请求
      if (this.loginForm.phonenumber == "" || this.loginForm.phonenumber == undefined || this.loginForm.phonenumber == null || this.loginForm.username == "" || this.loginForm.username == undefined || this.loginForm.username == null) {
        this.$message.warning("账号或手机号码不可为空");
        return;
      }
      let mobile_mode = /^1[23456789]\d{9}$/;
      if (!mobile_mode.test(this.loginForm.phonenumber)) {
        this.$message.warning("手机号码格式不正确");
        return;
      }
      let timestamp = new Date().getTime();
      let sign = md5("~|~" + timestamp + "~|~");
      let _this=this;
     /* let json="{" +
        "    \"data\": {" +
        "        \"phonenumber\": \""+_this.loginForm.phonenumber+"\"," +
        "        \"userName\": \""+_this.loginForm.username+"\"" +
        "    }," +
        "    \"timestamp\": \""+timestamp+"\"," +
        "    \"signType\": \"MD5\"," +
        "    \"sign\": \""+sign+"\"" +
        "}"*/
      captchaImageByPhoneNumber({
        timestamp: timestamp,
        signType: "MD5",
        sign: sign,
        data: {
          phonenumber: _this.loginForm.phonenumber,
          userName: _this.loginForm.username
        }
      }).then((res) => {
        this.loginForm.uuid = res.uuid;
        Cookies.set("uuid", res.uuid, {expires: 30});
        // 验证码倒计时
        if (!this.timer) {
          this.count = 60;
          this.show = false;
          this.timer = setInterval(() => {
            if (this.count > 0 && this.count <= 60) {
              this.count--;
            } else {
              this.show = true;
              clearInterval(this.timer);
              this.timer = null;
            }
          }, 1000);
        }
      });
    },
    /*getVerifyCode1() {
      //获取验证码
      if (this.validatePhone()) {
        this.validateBtn()
        //发送网络请求
      }
    },
    validatePhone() {
      //判断输入的手机号是否合法
      if (!this.phone) {
        this.errors = {
          phone: "手机号码不能为空"
        }
        // return false
      } else if (!/^1[345678]\d{9}$/.test(this.phone)) {
        this.errors = {
          phone: "请输入正确是手机号"
        }
        // return false
      } else {
        this.errors = {}
        return true
      }
    },*/
    /* validateBtn() {
       //倒计时
       let time = 60;
       let timer = setInterval(() => {
         if (time == 0) {
           clearInterval(timer);
           this.disabled = false;
           this.btnTitle = "获取验证码";
         } else {
           this.btnTitle = time + '秒后重试';
           this.disabled = true;
           time--
         }
       }, 1000)
     },*/
    getCode() {
      getCodeImg().then((res) => {
        this.codeUrl = "data:image/gif;base64," + res.img;
        this.loginForm.uuid = res.uuid;
      });
    },
    getCookie() {
      const username = Cookies.get("username");
      const password = Cookies.get("password");
      const uuid = Cookies.get("uuid");
      const rememberMe = Cookies.get("rememberMe");
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        uuid: uuid === undefined ? this.loginForm.uuid : uuid,
        password:
          password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe),
      };
    },
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          if (this.loginForm.rememberMe) {
            Cookies.set("username", this.loginForm.username, {expires: 30});
            Cookies.set("uuid", this.loginForm.uuid, {expires: 30});
            Cookies.set("password", encrypt(this.loginForm.password), {
              expires: 30,
            });
            Cookies.set("rememberMe", this.loginForm.rememberMe, {
              expires: 30,
            });
          } else {
            Cookies.remove("username");
            Cookies.remove("password");
            Cookies.remove("rememberMe");
          }
          if (this.loginForm.uuid === undefined || this.loginForm.uuid === null || this.loginForm.uuid === "") {
            const uuid = Cookies.get("uuid");
            this.loginForm.uuid = uuid;
          }
          this.$store
            .dispatch("Login", this.loginForm)
            .then(() => {
              this.$router.push({path: this.redirect || "index"});
            })
            .catch(() => {
              this.loading = false;
              // this.getCode();
            });
        }
      });
    },
  },
};
</script>

<style rel="stylesheet/scss" lang="scss">
/* 短信验证码css */
.pr {
  position: relative;
}

.code-btn {
  width: 100px;
  height: 20px;
  position: absolute;
  top: 10px;
  right: 5px;
  z-index: 222;
  color: #ef8466;
  font-size: 14px;
  border: none;
  border-left: 1px solid #bababa;
  padding-left: 16px;
  padding-top: 4px;
  background-color: #fff;
  cursor: pointer;
  border-radius: 0;
}

.code-btn:hover {
  color: #ef8466;
  background-color: #fff;
  border-left: 1px solid #bababa;
}

.login {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-image: url("../assets/image/login-bg.jpg");
  background-size: cover;
}

.title {
  //margin: 200px auto 30px auto;
  //text-align: center;
  //color: #510507;
  //font-size: 26px;


  margin-top: -100px;
  height: 181px;
  width: 362px;
  background-size: cover;
  background-position: 50%;
  background-image: url(../assets/image/logoBig.png);
  background-size: 362px 181px;
  background-repeat: no-repeat;
}

.login-detail {
  position: relative;
  width: 600px;
  height: 380px;
  background: url(../assets/image/box.png) no-repeat;
  background-size: 100% 100%;
}

.login-form {
  //border-radius: 6px;
  //background: rgba(255, 255, 255, .6);
  //width: 50%;
  //padding: 20px 5px 5px 20px;
  //display: flex;
  //margin-top: 0px;
  //position: relative;

  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  width: 291px;
  margin-top: -4px;

  .el-form-item {
    //width: 40%;
    margin-right: 10px;
  }

  .el-input {
    height: 38px;
    margin-right: 20px;

    input {
      height: 38px;
    }
  }

  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}

.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}

.login-code {
  width: 100px;
  height: 38px;
  position: absolute;
  right: 0;
  top: 0;
  border-radius: 6px;

  img {
    cursor: pointer;
    vertical-align: middle;
  }
}

.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}

.login-code-img {
  height: 38px;
}
</style>
